package com.peanut.baby.mvp.livedetail;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.mvp.main.expert.live.MainLiveListPresenter;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/7/21.
 */

public class LiveListPresenter implements LiveListContract.Presenter{
    private static final String TAG = MainLiveListPresenter.class.getSimpleName();
    private LiveListContract.View view;
    Activity    activity;
    public LiveListPresenter(LiveListContract.View view,Activity activity){
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getLiveList(final int pageNo, int pageSize) {
        Observable obs = RetrofitClient.getInstance().getLiveRoomList(InitManager.getInstance().getUserId(),pageNo,pageSize);
        obs.compose(BaseScheduler.<BaseResponse<List<LiveRoom>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<LiveRoom>>() {
                    @Override
                    protected void onHandleSuccess(List<LiveRoom> o) {
                        view.onLiveListGet(o);

                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onGetLiveListFailed(pageNo,msg);
                    }

                });
    }

    @Override
    public void joinRoom(final LiveRoom roomId) {
        Observable observable = RetrofitClient.getInstance().enroleLiveRoom(InitManager.getInstance().getUser().userToken,
                InitManager.getInstance().getUserId(),roomId.roomId+"");
        observable.compose(BaseScheduler.<BaseResponse<String>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onHandleSuccess(String o) {
                        view.onJoinSuccess(roomId);

                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onJoinFailed(code,msg);
                    }

                });
    }
}
